﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Text;
using System.IO;
using System.Xml;
using BLL;
using DAL;
using Models;
using System.Data;
using Web.Old_App_Code;

public partial class alipay_notify_url : System.Web.UI.Page
{
    // Fields
    protected ExpenseRecords er = new ExpenseRecords();
    protected ExpenseRecordsManager erm = new ExpenseRecordsManager();
    protected GroupBuyOrderManager gbom = new GroupBuyOrderManager();
    protected GoodsPromotionManager gpm = new GoodsPromotionManager();
    protected OrderManager om = new OrderManager();
    protected string remindSetXmlPath = "/xml/remind_set.xml";
    protected UserManager um = new UserManager();
    protected User user = new User();

    // Methods
    public SortedDictionary<string, string> GetRequestPost()
    {
        int index = 0;
        SortedDictionary<string, string> dictionary = new SortedDictionary<string, string>();
        string[] allKeys = base.Request.Form.AllKeys;
        for (index = 0; index < allKeys.Length; index++)
        {
            dictionary.Add(allKeys[index], base.Request.Form[allKeys[index]]);
        }
        return dictionary;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        SortedDictionary<string, string> requestPost = this.GetRequestPost();
        AlipayConfig config = new AlipayConfig();
        string partner = config.Partner;
        string key = config.Key;
        string str3 = config.Input_charset;
        string str4 = config.Sign_type;
        string transport = config.Transport;
        if (requestPost.Count > 0)
        {
            AlipayNotify notify = new AlipayNotify(requestPost, base.Request.Form["notify_id"], partner, key, str3, str4, transport);
            string responseTxt = notify.ResponseTxt;
            string str7 = base.Request.Form["sign"];
            string mysign = notify.Mysign;
            string sWord = "responseTxt=" + responseTxt + "\n notify_url_log:sign=" + base.Request.Form["sign"] + "&mysign=" + mysign + "\n notify回来的参数：" + notify.PreSignStr;
            AlipayFunction.log_result(base.Server.MapPath("log/" + DateTime.Now.ToString().Replace(":", "")) + ".txt", sWord);
            if ((responseTxt == "true") && (str7 == mysign))
            {
                DataTable table2;
                XmlDocument document;
                XmlElement element;
                XmlElement element2;
                string str17;
                SendMailMessage message;
                DataTable allEnabledMail;
                Random random;
                DataRow row;
                DataTable orderByOrderNo;
                DataRow row2;
                DataRow row3;
                DataTable userById = this.um.GetUserById((this.Session["user"] as User).Id);
                string str10 = base.Request.Form["trade_no"];
                string newValue = base.Request.Form["out_trade_no"];
                string str12 = base.Request.Form["total_fee"];
                string str13 = base.Request.Form["subject"];
                string str14 = base.Request.Form["body"];
                string str15 = base.Request.Form["buyer_email"];
                string str16 = base.Request.Form["trade_status"];
                if ((base.Request.Form["trade_status"] == "TRADE_FINISHED") || (base.Request.Form["trade_status"] == "TRADE_SUCCESS"))
                {
                    if ((str13 == "在线充值") && (str14 == "在线充值"))
                    {
                        this.er.OrderNo = newValue;
                        this.er.ExpenseUser = (this.Session["user"] as User).Id;
                        this.er.ExpenseRemark = str14;
                        this.er.ExpenseMoney = Convert.ToDecimal(str12);
                        this.er.ExpenseType = 0;
                        this.erm.AddExpenseRecords(this.er);
                        table2 = this.gpm.GetPresentedIntegral(6, this.er.ExpenseMoney, "zh-CN");
                        if (table2.Rows.Count > 0)
                        {
                            this.user.Id = this.er.ExpenseUser;
                            this.user.Money = Convert.ToDecimal(userById.Rows[0]["money"]) + this.er.ExpenseMoney;
                            this.user.PayPoints = Convert.ToInt32(userById.Rows[0]["payPoints"]) + Convert.ToInt32(table2.Rows[0]["presentedIntegral"]);
                            this.um.ModifyUserMoneyAndPayPoints(this.user);
                        }
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/online_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    else if (newValue.Contains("PM_"))
                    {
                        this.om.ModifyPayStatusByOrderNo(newValue, 1);
                        this.om.ModifyPaymentByOrderNo(newValue, "支付宝", "directPay");
                        orderByOrderNo = this.om.GetOrderByOrderNo(newValue);
                        if (orderByOrderNo.Rows.Count > 0)
                        {
                            row2 = orderByOrderNo.Rows[0];
                            row3 = userById.Rows[0];
                            this.user.Id = Convert.ToInt32(row3["id"]);
                            this.user.Money = Convert.ToDecimal(row3["money"]);
                            this.user.PayPoints = Convert.ToInt32(row3["payPoints"]) + Convert.ToInt32(row2["presentedIntegra"]);
                            this.um.ModifyUserMoneyAndPayPoints(this.user);
                        }
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/order_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    else if (newValue.Contains("GB_"))
                    {
                        this.gbom.ModifyPayStatusByOrderNo(newValue, 1);
                        this.gbom.ModifyPaymentByOrderNo(newValue, "支付宝", "directPay");
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/order_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "团购订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    base.Response.Write("success");
                }
                else
                {
                    if ((str13 == "在线充值") && (str14 == "在线充值"))
                    {
                        this.er.OrderNo = newValue;
                        this.er.ExpenseUser = (this.Session["user"] as User).Id;
                        this.er.ExpenseRemark = str14;
                        this.er.ExpenseMoney = Convert.ToDecimal(str12);
                        this.er.ExpenseType = 0;
                        this.erm.AddExpenseRecords(this.er);
                        table2 = this.gpm.GetPresentedIntegral(6, this.er.ExpenseMoney, "zh-CN");
                        if (table2.Rows.Count > 0)
                        {
                            this.user.Id = this.er.ExpenseUser;
                            this.user.Money = Convert.ToDecimal(userById.Rows[0]["money"]) + this.er.ExpenseMoney;
                            this.user.PayPoints = Convert.ToInt32(userById.Rows[0]["payPoints"]) + Convert.ToInt32(table2.Rows[0]["presentedIntegral"]);
                            this.um.ModifyUserMoneyAndPayPoints(this.user);
                        }
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/online_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    else if (newValue.Contains("PM_"))
                    {
                        this.om.ModifyPayStatusByOrderNo(newValue, 1);
                        this.om.ModifyPaymentByOrderNo(newValue, "支付宝", "directPay");
                        orderByOrderNo = this.om.GetOrderByOrderNo(newValue);
                        if (orderByOrderNo.Rows.Count > 0)
                        {
                            row2 = orderByOrderNo.Rows[0];
                            row3 = userById.Rows[0];
                            this.user.Id = Convert.ToInt32(row3["id"]);
                            this.user.Money = Convert.ToDecimal(row3["money"]);
                            this.user.PayPoints = Convert.ToInt32(row3["payPoints"]) + Convert.ToInt32(row2["presentedIntegra"]);
                            this.um.ModifyUserMoneyAndPayPoints(this.user);
                        }
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/order_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    else if (newValue.Contains("GB_"))
                    {
                        this.gbom.ModifyOrderStatusByOrderNo(newValue, 1);
                        this.gbom.ModifyPaymentByOrderNo(newValue, "支付宝", "directPay");
                        document = new XmlDocument();
                        document.Load(HttpContext.Current.Server.MapPath(this.remindSetXmlPath));
                        element = document.SelectSingleNode("set") as XmlElement;
                        element2 = document.SelectSingleNode("set").ChildNodes[1] as XmlElement;
                        str17 = File.ReadAllText(base.Server.MapPath("/cn/txt_template/order_payment.txt"));
                        if (element2.GetAttribute("mail_remind") == "true")
                        {
                            message = new SendMailMessage();
                            message.subject = "团购订单支付成功";
                            message.body = str17.Replace("[userName]", (this.Session["user"] as User).UserName).Replace("[orderNo]", newValue).Replace("[orderTotal]", string.Format("{0:C}", Convert.ToDecimal(str12)));
                            message.html = true;
                            allEnabledMail = new MailManager().GetAllEnabledMail();
                            random = new Random();
                            row = allEnabledMail.Rows[random.Next(allEnabledMail.Rows.Count - 1)];
                            message.from = row["fromAddress"].ToString();
                            message.fromName = row["fromName"].ToString();
                            message.mailDomain = row["mailDomain"].ToString();
                            message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                            message.mailServerUserName = row["mailServerUserName"].ToString();
                            message.mailServerPassWord = row["mailServerPassWord"].ToString();
                            if (element2.GetAttribute("notice_member") == "true")
                            {
                                message.mailTo = (this.Session["user"] as User).Email;
                                message.RuleSend();
                            }
                            if (element2.GetAttribute("notice_admin") == "true")
                            {
                                message.mailTo = element.GetAttribute("remind_email");
                                message.RuleSend();
                            }
                        }
                    }
                    base.Response.Write("success");
                }
            }
            else
            {
                base.Response.Write("fail");
            }
        }
        else
        {
            base.Response.Write("无通知参数");
        }
    }

}